﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.BLL;
using System.Data.Linq.SqlClient;

namespace ClassLibrary.DAL
{
    public class DALSanPham
    {

        public static List<BLL.BLLSanPham> GetAll()
        {

            var sps = (from sp in DALCommon.NonDB.SanPhams
                       select
                 new BLLSanPham(sp.MaSP,Convert.ToInt32(sp.IDLoai),sp.TenSP,sp.ChatLieu,sp.MauSac,Convert.ToDecimal(sp.Gia),sp.HinhAnh));
            return sps.ToList<BLLSanPham>();
        }
        public static List<BLL.BLLSanPham> GetByDanhMuc(BLLSanPham sanpham)
        {

            var sps = (from sp in DALCommon.NonDB.SanPhams
                       where sp.IDLoai==sanpham.Idloai
                       select
                 new BLLSanPham(sp.MaSP, Convert.ToInt32(sp.IDLoai), sp.TenSP, sp.ChatLieu, sp.MauSac, Convert.ToDecimal(sp.Gia), sp.HinhAnh));
            return sps.ToList<BLLSanPham>();
        }
        public static List<BLL.BLLSanPham> GetByID(BLLSanPham sanpham)
        {

            var sps = (from sp in DALCommon.NonDB.SanPhams
                       where sp.MaSP == sanpham.MaSP
                       select
                 new BLLSanPham(sp.MaSP, Convert.ToInt32(sp.IDLoai), sp.TenSP, sp.ChatLieu, sp.MauSac, Convert.ToDecimal(sp.Gia), sp.HinhAnh));
            return sps.ToList<BLLSanPham>();
        }

        public static List<BLL.BLLSanPham>Tim(BLLSanPham sanpham)
        {
            DataNonDataContext data = DALCommon.NonDB;
            var result = from sp in data.SanPhams
                         select sp;

            if (sanpham.TenSP != "")
            {
                result = result.Where(sp => SqlMethods.Like(sp.TenSP, "%" + sanpham.MaSP + "%"));
            }
            if (sanpham.Idloai != 0)
            {
                result = result.Where(sp => sp.IDLoai == sanpham.Idloai);
            }
            if (sanpham.Gia == "1")
            {
                result = result.Where(sp => Convert.ToDecimal(sp.Gia) <= 50000);
            }
            if (sanpham.Gia == "2")
            {
                result = result.Where(sp => Convert.ToDecimal(sp.Gia) > 50000 && Convert.ToInt32(sp.Gia) <= 100000);
            }
            if (sanpham.Gia == "3")
            {
                result = result.Where(sp => Convert.ToDecimal(sp.Gia) > 100000 && Convert.ToInt32(sp.Gia) <= 200000);
            }
            if (sanpham.Gia == "4")
            {
                result = result.Where(sp => Convert.ToDecimal(sp.Gia) > 200000);
            }
            List<BLLSanPham> list = new List<BLLSanPham>();
            foreach (SanPham sp in result)
            {
                list.Add(new BLLSanPham(sp.MaSP, Convert.ToInt32(sp.IDLoai), sp.TenSP, sp.ChatLieu, sp.MauSac, Convert.ToDecimal(sp.Gia), sp.HinhAnh));
            }

            return list;
        }

        public static string Add(BLLSanPham sanpham)
        {
          //  DataNonDataContext sp = new DataNonDataContext();
            SanPham sp = new SanPham();
            sp.MaSP = sanpham.MaSP;
            sp.TenSP = sanpham.TenSP;
            sp.MauSac = sanpham.MauSac;
            sp.ChatLieu = sanpham.ChatLieu;
            sp.HinhAnh = sanpham.HinhAnh;
            sp.Gia =Convert.ToDecimal(sanpham.Gia);
            sp.IDLoai = Convert.ToInt32(sanpham.Idloai);
            DALCommon.NonDB.SanPhams.InsertOnSubmit(sp);
            DALCommon.NonDB.SubmitChanges();
            return sp.MaSP;

        }

        public static void Delete(BLLSanPham sanpham)
        {
            SanPham sp = DALCommon.NonDB.SanPhams.Single<SanPham>(n => n.MaSP == sanpham.MaSP);
            DALCommon.NonDB.SanPhams.DeleteOnSubmit(sp);
            DALCommon.NonDB.SubmitChanges();
        }
        public static void Update(BLLSanPham sanpham)
        {
            SanPham sp = DALCommon.NonDB.SanPhams.Single<SanPham>(d => d.MaSP== sanpham.MaSP);
            sp.MaSP = sanpham.MaSP;
            sp.TenSP = sanpham.TenSP;
            sp.MauSac = sanpham.MauSac;
            sp.Gia = Convert.ToDecimal(sanpham.Gia);
            sp.ChatLieu = sanpham.ChatLieu;
            sp.HinhAnh = sanpham.HinhAnh;
            sp.IDLoai = Convert.ToInt32(sanpham.Idloai);
            DALCommon.NonDB.SubmitChanges();
        }
        //public static List<BLL.BLLSanPham> GetByID(BLLSanPham sanpham)
        //{

        //    var sps = (from sp in DALCommon.NonDB.SanPhams
        //               where sp.MaSP==sanpham.MaSP
        //               select
        //         new BLLSanPham(sp.MaSP, Convert.ToInt32(sp.IDLoai), sp.TenSP, sp.ChatLieu, sp.MauSac, Convert.ToDecimal(sp.Gia), sp.HinhAnh));
        //    return sps.ToList<BLLSanPham>();
        //}


        //lay 6 sp moi' trong trang default
    //    public static List<BLL.BLLSanPham> Get6()
    //    {

    //        var sps = (from sp in DALCommon.NonDB.SanPhams
    //                  select
    //            new BLLSanPham(sp.IDSanPham,sp.IDLoaiSanPham, sp.TenSanPham,sp.KieuDang,sp.ChatLieu,sp.KickCo,sp.Mau,sp.XuatXu,sp.DonGia.ToString(),sp.Hinh)).Take(6);

    //        return sps.ToList<BLLSanPham>();           
    //    }
    //    //sp theo danh muc
    //    public static List<BLL.BLLSanPham> GetSPDM(BLLSanPham bllsp)
    //    {
    //        var sps = (from sp in DALCommon.NonDB.SanPhams
    //                   where sp.IDLoaiSanPham.ToString()==bllsp.Idloai
    //                   select
    //             new BLLSanPham(sp.IDSanPham, sp.IDLoaiSanPham, sp.TenSanPham, sp.KieuDang, sp.ChatLieu, sp.KickCo, sp.Mau, sp.XuatXu, sp.DonGia.ToString(), sp.Hinh));

    //        return sps.ToList<BLLSanPham>();
            
    //    }

    //    public static List<BLL.BLLSanPham> GetSPTK(BLLSanPham sanpham)
    //    {
    //        TMDTDataContext data = DALCommon.NonDB;
    //        var result = from sp in data.SanPhams
    //                     select sp;

    //        if (sanpham.Tensp != "")
    //        {
    //            result = result.Where(sp =>SqlMethods.Like(sp.TenSanPham,"%"+sanpham.Tensp+"%"));
    //        }
    //        if (sanpham.Idloai !="0")
    //        {
    //            result = result.Where(sp=>sp.IDLoaiSanPham==sanpham.Idloai);
    //        }
    //        if (sanpham.Dongia == "1")
    //        {
    //            result = result.Where(sp=>Convert.ToDecimal(sp.DonGia)<=50000);
    //        }
    //        if (sanpham.Dongia == "2")
    //        {
    //            result = result.Where(sp => Convert.ToDecimal(sp.DonGia) > 50000 && Convert.ToInt32(sp.DonGia) <=100000);
    //        }
    //        if (sanpham.Dongia == "3")
    //        {
    //            result = result.Where(sp => Convert.ToDecimal(sp.DonGia) > 100000 && Convert.ToInt32(sp.DonGia) <= 200000);
    //        }
    //        if (sanpham.Dongia == "4")
    //        {
    //            result = result.Where(sp =>Convert.ToDecimal(sp.DonGia) > 200000);
    //        }
    //        List<BLLSanPham> list = new List<BLLSanPham>();
    //        foreach (SanPham sp in result)
    //        {
    //            list.Add(new BLLSanPham(sp.IDSanPham, sp.IDLoaiSanPham, sp.TenSanPham, sp.KieuDang, sp.ChatLieu, sp.KickCo, sp.Mau, sp.XuatXu, sp.DonGia.ToString(), sp.Hinh));
    //        }

    //            return list;
    //    }

    //    // hien thi chi tiet san pham
    //    public static List<BLL.BLLSanPham> ShowChiTiet(BLLSanPham sanpham)
    //    {
    //        var sps = (from sp in DALCommon.NonDB.SanPhams
    //                   where sp.IDSanPham ==sanpham.Idsp
    //                   select
    //             new BLLSanPham(sp.IDSanPham, sp.IDLoaiSanPham, sp.TenSanPham, sp.KieuDang, sp.ChatLieu, sp.KickCo, sp.Mau, sp.XuatXu, sp.DonGia.ToString(), sp.Hinh));
    //        return sps.ToList<BLLSanPham>();
    //    }

    //    //them san pham moi
    //    public static string AddSanPham(BLLSanPham sanpham)
    //    {
    //        TMDTDataContext tmdt = new TMDTDataContext();
    //        SanPham sp = new SanPham();
    //        sp.IDSanPham = sanpham.Idsp;
    //        sp.IDLoaiSanPham = sanpham.Idloai;
    //        sp.TenSanPham = sanpham.Tensp;
    //        sp.KieuDang = sanpham.Kieudang;
    //        sp.ChatLieu = sanpham.Chatlieu;
    //        sp.KickCo = sanpham.Kichco;
    //        sp.Mau = sanpham.Mau;
    //        sp.XuatXu = sanpham.Xuatxu;
    //        sp.DonGia = sanpham.Dongia;
    //        sp.Hinh = sanpham.Hinh;
    //        DALCommon.NonDB.SanPhams.InsertOnSubmit(sp);
    //        try
    //        {
    //            DALCommon.NonDB.SubmitChanges();
    //        }
    //        catch (Exception ex)
    //        { 
                
    //        }
    //        return sp.IDSanPham;
            
    //    }
    //    //sua san pham
    //    public static string Edit(BLLSanPham sanpham)
    //    {
    //        SanPham sp = DALCommon.NonDB.SanPhams.Single<SanPham>(d => d.IDSanPham ==sanpham.Idsp);
    //        sp.IDLoaiSanPham=sanpham.Idloai;
    //        sp.KickCo=sanpham.Kichco;
    //        sp.KieuDang=sanpham.Kieudang;
    //        sp.Mau=sanpham.Mau;
    //        sp.XuatXu = sanpham.Xuatxu;
    //        sp.TenSanPham=sanpham.Tensp;
    //        sp.ChatLieu=sanpham.Chatlieu;
    //        sp.DonGia=sanpham.Dongia;
    //        sp.Hinh = sanpham.Hinh;
    //        DALCommon.NonDB.SubmitChanges();
    //        return sp.IDSanPham;
    //    }
    //    //xóa sản phẩm
    //    public static void Delete(BLLSanPham sanpham)
    //    {
    //        SanPham sp = DALCommon.NonDB.SanPhams.Single<SanPham>(n => n.IDSanPham == sanpham.Idsp);
    //        DALCommon.NonDB.SanPhams.DeleteOnSubmit(sp);
    //        DALCommon.NonDB.SubmitChanges();
    //    }
    //    //lay san pham theo id
    //    public static List<BLL.BLLSanPham> GetbyId(BLLSanPham sanpham)
    //    {
    //        var sps = (from sp in DALCommon.NonDB.SanPhams
    //                   where sp.IDSanPham == sanpham.Idsp
    //                   select
    //             new BLLSanPham(sp.IDSanPham, sp.IDLoaiSanPham, sp.TenSanPham, sp.KieuDang, sp.ChatLieu, sp.KickCo, sp.Mau, sp.XuatXu, sp.DonGia.ToString(), sp.Hinh));

    //        return sps.ToList<BLLSanPham>();
    //    }
    }
}
